/**
 * 百度地图插件
 * user:CaoBin
 * date:2016-08-22
 * comp:YF
 */
;
(function ($, window, document, undefined) {
    var YFMap = function (ele, opt) {
        this.$element = ele;
        this.defaults = {
            enableAutoResize: true
        };
        this.options = $.extend({}, this.defaults, opt);
        this.map = new BMap.Map(this.$element.attr('id'), this.options);
        this.cluster;
        this.WEB_API_KEY = "0749cba04b7af237211ec92a9e58cdc6";
        this.infoWindow = new BMap.InfoWindow({offset: new BMap.Pixel(-10, -10)});
    }
    YFMap.prototype = {
        init: function () {
            this.centerAndZoom();
            this.map.enableScrollWheelZoom();
            this.ipLocate();
            this.addControl();
            return this;
        },
        centerAndZoom: function () {
            var point = new BMap.Point(104.065714, 30.657413);  // 创建点坐标
            this.map.centerAndZoom(point, 12);                 // 初始化地图，设置中心点坐标和地图级别
            return this;
        },
        addControl: function () {
            var scaleControl = new BMap.ScaleControl();
            var navigationControl = new BMap.NavigationControl();
            var overviewMapControl = new BMap.OverviewMapControl();

            this.map.addControl(scaleControl);
            this.map.addControl(navigationControl);
            this.map.addControl(overviewMapControl);

        },
        //获取用户所在城市信息
        ipLocate: function () {
            var that = this;
            var myCity = new BMap.LocalCity();
            myCity.get(function (result) {
                var cityName = result.name;
                that.map.setCenter(cityName);
            });
        },
        panTo: function (x, y) {
            var lonlat = new BMap.Point(x, y);
            this.map.panTo(lonlat);
        },
        //以下待测试
        addMarker: function (x, y, imgUrl, extData) {
            var marker= new BMap.Marker({
                point: new BMap.Point(x, y),//基点位置
                icon: new BMap.Icon(imgUrl),
                extData: extData,
                offset: new BMap.Pixel(-26, -8) //相对于基点的位置
            });
            this.map.addOverlay(marker);
            return this;
        },
        setMakerLabel: function (marker, text) {
            marker.setLabel(new BMap.Label(text,{offset:new BMap.Pixel(25, 0)}));
            return this;
        }
    }
    $.fn.yfmap = function (options) {
        if (window.YFMap) {
            return window.YFMap;
        } else {
            var yfMap = new YFMap(this, options);
            window.YFMap = yfMap;
            return yfMap.init();
        }
    }
})(jQuery, window, document);


